#include "dynamicxwidget.h"
#include "limiterwidget.h"
#include "compressorwidget.h"
#include "noisegatewidget.h"



DynamicXWidget::DynamicXWidget(const double &maxThreshold, QWidget *parent)
    : ATabWidget{parent}
    , m_maxThreshold{maxThreshold}
{
    setFixedSize(360, 420);
}

int DynamicXWidget::newTab(const DynamicX &type)
{
    if (DynamicX::NoiseGate == type) {
        auto widget = new NoiseGateWidget(this);
        connect(widget, &NoiseGateWidget::stateChanged, this, &DynamicXWidget::stateChanged);
        return addTab(widget, tr(LABEL_NOISE_GATE));
    }
    else if (DynamicX::Limiter == type) {
        auto widget = new LimiterWidget(m_maxThreshold, this);
        connect(widget, &LimiterWidget::stateChanged, this, &DynamicXWidget::stateChanged);
        return addTab(widget, tr(LABEL_LIMITER));
    }
    else if (DynamicX::Compressor == type) {
        auto widget = new CompressorWidget(m_maxThreshold, this);
        connect(widget, &CompressorWidget::stateChanged, this, &DynamicXWidget::stateChanged);
        return addTab(widget, tr(LABEL_COMPRESSOR));
    }
    return -1;
}

void DynamicXWidget::set(const DXVariant &dx)
{
    std::visit(
        overloaded{
            [this](const NoiseGate &param) { updateWidget<NoiseGateWidget>(param); },
            [this](const Limiter &param) { updateWidget<LimiterWidget>(param); },
            [this](const Compressor &param) { updateWidget<CompressorWidget>(param); }
        }, dx);
}

template<typename WidgetType, typename ParamType>
void DynamicXWidget::updateWidget(const ParamType &param)
{
    for (int i = 0; i < count(); ++i) {
        if (auto w = qobject_cast<WidgetType*>(widget(i))) {
            w->set(param);
            break;
        }
    }
}
